package com.sun.myproject.tool;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.joda.time.DateTime;

import com.sun.myproject.bean.Book;

public class ExcelUtil {
	public List<Object> ExcelUtil(FileInputStream inputStream) throws Exception {//工具类
		List<Object> list=new ArrayList<Object>();
		Workbook wb = new HSSFWorkbook(inputStream);
		// 1得到表
		Sheet sheet = wb.getSheetAt(0);

		// 获取标题内容
		Row rowTitle = sheet.getRow(0);
		if (rowTitle != null) {
			int cellCount = rowTitle.getPhysicalNumberOfCells();// 获取列数
			for (int cellNum = 0; cellNum < cellCount; cellNum++) {
				Cell cell = rowTitle.getCell(cellNum);
				if (cell != null) {
					// int cellType=cell.getCellType();
					String cellValue = cell.getStringCellValue();
					System.out.print(cellValue + "|");
				}
			}
			System.out.println();
		}
		// 获取表中的数据
		int rowCount = sheet.getPhysicalNumberOfRows();// 获取行数
		for (int rowNum = 1; rowNum < rowCount; rowNum++) {
			Row rowData = sheet.getRow(rowNum);
			if (rowData != null) {
				// 读取列
				int cellCount = rowTitle.getPhysicalNumberOfCells();
				for (int cellNum = 0; cellNum < cellCount; cellNum++) {
					System.out.print("[" + (rowNum + 1) + "-" + (cellNum + 1) + "]");
					Cell cell = rowData.getCell(cellNum);
					// 匹配列的数据类型
				
					
					if (cell != null) {
						
						int cellType = cell.getCellType();
						String cellValue = "";
						switch (cellType) {
						case HSSFCell.CELL_TYPE_STRING:// 字符串
							System.out.print("[string]");
							cellValue = cell.getStringCellValue();
							list.add(cellValue);
							break;
						case HSSFCell.CELL_TYPE_BOOLEAN:// 布尔
							System.out.print("boolean");
							cellValue = String.valueOf(cell.getBooleanCellValue());
							list.add(cellValue);
							break;
						case HSSFCell.CELL_TYPE_BLANK:// 空
							System.out.print("blank");
							list.add(cellValue);
							break;
						case HSSFCell.CELL_TYPE_NUMERIC:// 数字(日期，普通数字)
							System.out.print("NUMERIC");
							if (HSSFDateUtil.isCellInternalDateFormatted(cell)) {// 日期
								System.out.print("日期");
								Date date = cell.getDateCellValue();
								cellValue = new DateTime(date).toString("yyyy-MM-dd");
								list.add(cellValue);

							} else {// 数字转换为字符串
									//
								System.out.print("数字转换为字符串");
								cell.setCellType(HSSFCell.CELL_TYPE_STRING);
								cellValue = cell.toString();
								list.add(cellValue);
							}

							break;
						case HSSFCell.CELL_TYPE_ERROR:// 布尔
							System.out.println("数据类型error");
							
							break;
						}
						System.out.println(cellValue);
						
					}
				}
			}
		}

		// 关闭流
		inputStream.close();
		return list;
	}

}
